package cn.lixu.product.controller;

import cn.lixu.basic.annotation.PreAuthorize;
import cn.lixu.basic.exception.BusinessException;
import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.LoginContext;
import cn.lixu.basic.util.PageList;
import cn.lixu.org.domain.Logininfo;
import cn.lixu.product.domain.Product;
import cn.lixu.product.query.ProductQuery;
import cn.lixu.product.service.IProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    @PreAuthorize(name = "查询全部宠物列表", sn = "product:list") //value后面用来做权限校验
    public List<Product> loadAll(HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return productService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个")
    @PreAuthorize(name = "查询一个宠物列表", sn = "product:loadById") //value后面用来做权限校验
    public Product loadById(@PathVariable("id") Long id) {
        return productService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            productService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param product 添加和修改
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody Product product) {
        try {
            if (product.getId() == null) {
                productService.save(product);
            } else {
                productService.update(product);
            }
            return new JsonResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param productQuery
     * @return
     */
    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery productQuery) {
        return productService.queryPage(productQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            productService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 宠物上架
     *
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public JsonResult onsale(@RequestBody List<Long> ids) {
        try {
            productService.onSale(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 宠物下架
     *
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public JsonResult offsale(@RequestBody List<Long> ids) {
        try {
            productService.offSale(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }


//    /**
//     * 查询宠物树
//     *
//     * @return
//     */
//    @GetMapping("/deptTree")
//    @ApiOperation(value = "获取宠物树-无限极接口")
//    public List<Product> deptTree() {
//        return productService.deptTree();
//    }
}

